package com.menglei.dailytasks.m0718;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import androidx.preference.CheckBoxPreference;
import androidx.preference.EditTextPreference;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;

import com.menglei.dailytasks.R;


public class PrefFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener {
    private static final String TAG = "PrefFragment:";
    private EditTextPreference etIdPreference;
    private EditTextPreference etNamePreference;
    private ListPreference ltSexPreference;
    private CheckBoxPreference cbPreference;
    private boolean allow;
    private TextView mMainActivityContent;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.preferences);
        initView();
        allow = getPreferenceManager().getSharedPreferences().getBoolean("my_allow", false);
        setEnabled(allow);

    }

    private void initView() {
        cbPreference = (CheckBoxPreference) findPreference("my_allow");
        etIdPreference = (EditTextPreference) findPreference("my_id");
        etIdPreference.setOnPreferenceChangeListener(this);
        etNamePreference = (EditTextPreference) findPreference("my_name");
        etNamePreference.setOnPreferenceChangeListener(this);
        ltSexPreference = (ListPreference) findPreference("my_sex");
        ltSexPreference.setOnPreferenceChangeListener(this);
        mMainActivityContent = getActivity().findViewById(R.id.content);
        setTextContent(-1, "");
    }

    private void setTextContent(int index, String str) {
        String myId = getPreferenceManager().getSharedPreferences().getString("my_id", "无数据");
        String myName = getPreferenceManager().getSharedPreferences().getString("my_name", "无数据");
        String mySex = getPreferenceManager().getSharedPreferences().getString("my_sex", "男");
        if (index != -1) {
            switch (index) {
                case 0:
                    myId = str;
                    break;
                case 1:
                    myName = str;
                    break;
                case 2:
                    mySex = str;
                    break;
            }
        }
        String content = "当前数据：\n" +
                "工号：" + myId + "\n" +
                "姓名:\t" + myName + "\n" +
                "性别:\t" + mySex + "\n";
        mMainActivityContent.setText(content.trim());
    }

    /**
     * 根据allow状态去设置是否可以点击
     */
    private void setEnabled(boolean allow) {
        if (allow) {
            allowNotify(true);
        } else {
            allowNotify(false);
        }
    }

    /**
     * 设置是否可以点击
     */
    private void allowNotify(boolean allow) {
        etIdPreference.setEnabled(allow);
        etNamePreference.setEnabled(allow);
        ltSexPreference.setEnabled(allow);
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference.getKey().equals("my_allow")) {
            SharedPreferences sp = preference.getSharedPreferences();
            allow = sp.getBoolean("my_allow", false);
            setEnabled(allow);
            Log.d(TAG, preference.getKey() + ":" + allow);
        }
        Log.d(TAG, preference.getKey());
        return super.onPreferenceTreeClick(preference);
    }


    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        Log.d(TAG, newValue.toString());
        switch (preference.getKey()) {
            case "my_id":
                setTextContent(0, newValue.toString());
                break;
            case "my_name":
                setTextContent(1, newValue.toString());
                break;
            case "my_sex":
                setTextContent(2, newValue.toString());
                break;
        }
        return true;
    }
}
